//
//  VerificationCodeVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/29.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "VerificationCodeVC.h"
#import "LoginVM.h"
#import "HomeVC.h"
#import "AppDelegate.h"
#import "NewPasswordVC.h"
@interface VerificationCodeVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *labPhone;
@property (weak, nonatomic) IBOutlet UITextField *textVerification;
@property (nonatomic ,strong) QXLable *lab1;
@property (nonatomic ,strong) QXLable *lab2;
@property (nonatomic ,strong) QXLable *lab3;
@property (nonatomic ,strong) QXLable *lab4;
@property (nonatomic ,strong) UIButton  *btnTiming;
@end

@implementation VerificationCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.type==VerificationCodeTypeRegistered) {
        self.title=@"用户注册";
    }else{
        self.title=@"找回密码";
    }
    
    self.view.backgroundColor=[UIColor whiteColor];
    
    [self createUI];
    _labPhone.text=[NSString stringWithFormat:@"已向手机%@发送验证码",[self.userInfo objectForKey:@"Phone"]];
    _textVerification.inputAccessoryView=self.btnTiming;
    [self start];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [_textVerification becomeFirstResponder];
}

- (void)start{
    WS(weakSelf);
    self.btnTiming.userInteractionEnabled=NO;
    [self.btnTiming setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
    [self.btnTiming setImage:nil forState:UIControlStateNormal];
    
    [[LoginVM manage] getVerificationCode:[self.userInfo objectForKey:@"Phone"] andSuccess:^(id returnValue) {
        [weakSelf.btnTiming setTitle:@" 重新发送" forState:UIControlStateNormal];
        [weakSelf.btnTiming setTitleColor:NAVCOLOR forState:UIControlStateNormal];
        [weakSelf.btnTiming setImage:[UIImage imageNamed:@"Login_refresh"] forState:UIControlStateNormal];
        weakSelf.btnTiming.userInteractionEnabled=YES;
    } andContinue:^(id returnValue) {
        [weakSelf.btnTiming setTitle:returnValue forState:UIControlStateNormal];
    }];
}

- (void)createUI{
    [self.view addSubview:self.lab1];
    [self.lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(real_size(20));
        make.top.equalTo(self.textVerification);
        make.size.mas_equalTo(CGSizeMake(real_size(61), real_size(61)));
    }];
    
    [self.view addSubview:self.lab2];
    [self.lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lab1.mas_right).with.offset(real_size(30));
        make.top.equalTo(self.textVerification);
        make.size.mas_equalTo(CGSizeMake(real_size(61), real_size(61)));
    }];
    
    [self.view addSubview:self.lab3];
    [self.lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lab2.mas_right).with.offset(real_size(30));
        make.top.equalTo(self.textVerification);
        make.size.mas_equalTo(CGSizeMake(real_size(61), real_size(61)));
    }];
    
    [self.view addSubview:self.lab4];
    [self.lab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).with.offset(-real_size(20));
        make.top.equalTo(self.textVerification);
        make.size.mas_equalTo(CGSizeMake(real_size(61), real_size(61)));
    }];
}



- (void)selectLab:(QXLable *)lab{
    self.lab1.layer.borderColor=DEFAULTBGCOLOR.CGColor;
    self.lab2.layer.borderColor=DEFAULTBGCOLOR.CGColor;
    self.lab3.layer.borderColor=DEFAULTBGCOLOR.CGColor;
    self.lab4.layer.borderColor=DEFAULTBGCOLOR.CGColor;
    lab.layer.borderColor=NAVCOLOR.CGColor;
}

- (void)submitRegister:(NSString *)code{
    if (self.type==VerificationCodeTypeRegistered) {
        [self.userInfo setObject:code forKey:@"code"];
        [[LoginVM manage] registerWithUserInfo:self.userInfo andSuccess:^(id returnValue) {
            [SVProgressHUD showSuccessWithStatus:@"注册成功"];
            
            [self.navigationController popToRootViewControllerAnimated:YES];

            
        } andFail:^(NSString *errorMessage, NSError *error) {
            
        }];
    }else if (self.type==VerificationCodeTypeForgetPassword){
        [self.userInfo setObject:code forKey:@"Code"];
        NewPasswordVC *vc=[[UIStoryboard storyboardWithName:@"CLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"NewPasswordVC"];
        vc.userInfo=self.userInfo;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark -- 懒加载
- (QXLable *)lab1{
    if (!_lab1) {
        _lab1 =[[QXLable alloc]init];
        _lab1.textAlignment=NSTextAlignmentCenter;
        _lab1.font=[UIFont systemFontOfSize:35];
        _lab1.backgroundColor=DEFAULTBGCOLOR;
        _lab1.layer.masksToBounds=YES;
        _lab1.layer.cornerRadius=4;
        _lab1.layer.borderWidth=1.5;
        _lab1.layer.borderColor=NAVCOLOR.CGColor;
    }
    return _lab1;
}

- (QXLable *)lab2{
    if (!_lab2) {
        _lab2 =[[QXLable alloc]init];
        _lab2.textAlignment=NSTextAlignmentCenter;
        _lab2.font=[UIFont systemFontOfSize:35];
        _lab2.backgroundColor=DEFAULTBGCOLOR;
        _lab2.layer.masksToBounds=YES;
        _lab2.layer.cornerRadius=4;
        _lab2.layer.borderWidth=1.5;
        _lab2.layer.borderColor=DEFAULTBGCOLOR.CGColor;
    }
    return _lab2;
}

- (QXLable *)lab3{
    if (!_lab3) {
        _lab3 =[[QXLable alloc]init];
        _lab3.textAlignment=NSTextAlignmentCenter;
        _lab3.font=[UIFont systemFontOfSize:35];
        _lab3.backgroundColor=DEFAULTBGCOLOR;
        _lab3.layer.masksToBounds=YES;
        _lab3.layer.cornerRadius=4;
        _lab3.layer.borderWidth=1.5;
        _lab3.layer.borderColor=DEFAULTBGCOLOR.CGColor;
    }
    return _lab3;
}

- (QXLable *)lab4{
    if (!_lab4) {
        _lab4 =[[QXLable alloc]init];
        _lab4.textAlignment=NSTextAlignmentCenter;
        _lab4.font=[UIFont systemFontOfSize:35];
        _lab4.backgroundColor=DEFAULTBGCOLOR;
        _lab4.layer.masksToBounds=YES;
        _lab4.layer.cornerRadius=4;
        _lab4.layer.borderWidth=1.5;
        _lab4.layer.borderColor=DEFAULTBGCOLOR.CGColor;
    }
    return _lab4;
}

- (UIButton *)btnTiming{
    if (!_btnTiming) {
        _btnTiming=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        [_btnTiming.titleLabel setFont:[UIFont systemFontOfSize:15]];
        _btnTiming.userInteractionEnabled=NO;
        [_btnTiming setTitleColor:DEFAULTTEXTBLACKCOLOR forState:UIControlStateNormal];
        [_btnTiming addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnTiming;
}

#pragma mark -- 懒加载
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *str=textField.text;
    if (str.length==0) {
        [self selectLab:self.lab2];
        self.lab1.text=string;
    }else if (str.length==1){
        if (string.length==0) {
            [self selectLab:self.lab1];
            self.lab1.text=string;
        }else{
            [self selectLab:self.lab3];
            self.lab2.text=string;
        }
        
    }else if (str.length==2){
        if (string.length==0) {
            [self selectLab:self.lab2];
            self.lab2.text=string;
        }else{
            [self selectLab:self.lab4];
            self.lab3.text=string;
        }
        
    }else if (str.length==3){
        if (string.length==0) {
            [self selectLab:self.lab3];
            self.lab3.text=string;
        }else{
            
            [self submitRegister:[NSString stringWithFormat:@"%@%@",str,string]];
            [textField resignFirstResponder];
            
            self.lab4.text=string;
        }
    }else{
        if (string.length==0) {
            [self selectLab:self.lab3];
            self.lab4.text=string;
        }else{
            
            return NO;
        }
        
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
